//
//  DisplayStyleAlert.m
//  PersonalOCTest
//
//  Created by zlf on 2025/4/22.
//

#import "DisplayStyleAlert.h"

@interface DisplayStyleAlert()

@property (nonatomic, strong) UIView *containerView;    // 内容容器视图
@property (nonatomic, strong) UIView *overlayView;         // 蒙版视图

@property (nonatomic, strong) UILabel *titleLabel;      // 标题
@property (nonatomic, strong) UILabel *descriptionLabel; // 描述文本
@property (nonatomic, strong) UIView *screenDisplayView; // 息屏显示视图
@property (nonatomic, strong) UILabel *screenDisplayLabel; // 息屏显示标签
@property (nonatomic, strong) UISwitch *screenDisplaySwitch; // 息屏显示开关
@property (nonatomic, strong) UILabel *screenDisplayTipLabel; // 息屏显示提示文本

@property (nonatomic, strong) UILabel *displayAsLabel;  // "显示为"标签
@property (nonatomic, strong) NSArray *modeViews;       // 模式视图数组
@property (nonatomic, strong) NSArray *modeLabels;      // 模式标签数组
@property (nonatomic, strong) UILabel *tipLabel;        // 提示文本

@property (nonatomic, strong) UIButton *confirmButton;  // 确认按钮
@property (nonatomic, strong) UIButton *cancelButton;   // 取消按钮

@end

@implementation DisplayStyleAlert

- (instancetype)init {
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        self.selectedDisplayMode = 0; // 默认选择简洁模式
        self.isScreenDisplayEnabled = YES; // 默认息屏显示开启
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    // 设置蒙版视图
    self.overlayView = [[UIView alloc] initWithFrame:self.bounds];
        self.overlayView.backgroundColor = [UIColor blackColor];
        self.overlayView.alpha = 0.4;
        [self addSubview:self.overlayView];
    
    // 设置容器视图
    self.containerView = [[UIView alloc] init];
    self.containerView.backgroundColor = [UIColor whiteColor];
    self.containerView.layer.cornerRadius = 16;
    [self addSubview:self.containerView];
    
    // 标题
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"显示样式";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    [self.containerView addSubview:self.titleLabel];
    
    // 描述文本
    self.descriptionLabel = [[UILabel alloc] init];
    self.descriptionLabel.text = @"设置收到 微信 消息时，眼镜上的显示样式";
    self.descriptionLabel.textAlignment = NSTextAlignmentCenter;
    self.descriptionLabel.font = [UIFont systemFontOfSize:14];
    self.descriptionLabel.textColor = [UIColor darkGrayColor];
    [self.containerView addSubview:self.descriptionLabel];
    
    // 息屏显示视图
    self.screenDisplayView = [[UIView alloc] init];
    self.screenDisplayView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
    self.screenDisplayView.layer.cornerRadius = 12;
    [self.containerView addSubview:self.screenDisplayView];
    
    // 息屏显示标签
    self.screenDisplayLabel = [[UILabel alloc] init];
    self.screenDisplayLabel.text = @"息屏显示";
    self.screenDisplayLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [self.screenDisplayView addSubview:self.screenDisplayLabel];
    
    // 息屏显示提示
    self.screenDisplayTipLabel = [[UILabel alloc] init];
    self.screenDisplayTipLabel.text = @"文案待确认@PD";
    self.screenDisplayTipLabel.font = [UIFont systemFontOfSize:12];
    self.screenDisplayTipLabel.textColor = [UIColor lightGrayColor];
    [self.screenDisplayView addSubview:self.screenDisplayTipLabel];
    
    // 息屏显示开关
    self.screenDisplaySwitch = [[UISwitch alloc] init];
    self.screenDisplaySwitch.on = self.isScreenDisplayEnabled;
    self.screenDisplaySwitch.onTintColor = [UIColor colorWithRed:0/255.0 green:217/255.0 blue:147/255.0 alpha:1.0];
    [self.screenDisplaySwitch addTarget:self action:@selector(screenSwitchChanged:) forControlEvents:UIControlEventValueChanged];
    [self.screenDisplayView addSubview:self.screenDisplaySwitch];
    
    // 显示为标签
    self.displayAsLabel = [[UILabel alloc] init];
    self.displayAsLabel.text = @"显示为";
    self.displayAsLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [self.containerView addSubview:self.displayAsLabel];
    
    // 模式视图
    NSArray *titles = @[@"简洁", @"详细", @"智能折叠"];
    NSMutableArray *views = [NSMutableArray array];
    NSMutableArray *labels = [NSMutableArray array];
    
    CGFloat width = (self.bounds.size.width - 80) / 3;
    
    for (NSInteger i = 0; i < titles.count; i++) {
        UIView *modeView = [[UIView alloc] init];
        modeView.layer.cornerRadius = 8;
        modeView.layer.borderWidth = 1;
        modeView.tag = i;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(modeViewTapped:)];
        [modeView addGestureRecognizer:tap];
        
        [self.containerView addSubview:modeView];
        [views addObject:modeView];
        
        // 模式示例视图
        UIView *exampleView = [[UIView alloc] init];
        if (i == 0) { // 简洁
            exampleView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
            exampleView.layer.cornerRadius = 4;
        } else if (i == 1) { // 详细
            exampleView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
            exampleView.layer.cornerRadius = 4;
        } else { // 智能折叠
            exampleView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
            exampleView.layer.cornerRadius = 4;
        }
        [modeView addSubview:exampleView];
        
        // 模式标签
        UILabel *label = [[UILabel alloc] init];
        label.text = titles[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:14];
        [self.containerView addSubview:label];
        [labels addObject:label];
    }
    
    self.modeViews = views;
    self.modeLabels = labels;
    
    // 提示文本
    self.tipLabel = [[UILabel alloc] init];
    self.tipLabel.text = @"收到长消息时，眼镜默认折叠内容，「简洁」显示";
    self.tipLabel.font = [UIFont systemFontOfSize:12];
    self.tipLabel.textColor = [UIColor lightGrayColor];
    self.tipLabel.textAlignment = NSTextAlignmentCenter;
    [self.containerView addSubview:self.tipLabel];
    
    // 确认按钮
    self.confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.confirmButton setTitle:@"确认" forState:UIControlStateNormal];
    [self.confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.confirmButton.backgroundColor = [UIColor colorWithRed:0/255.0 green:217/255.0 blue:147/255.0 alpha:1.0];
    self.confirmButton.layer.cornerRadius = 22;
    [self.confirmButton addTarget:self action:@selector(confirmButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.containerView addSubview:self.confirmButton];
    
    // 取消按钮
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [self.containerView addSubview:self.cancelButton];
    
    [self updateSelectedMode];
    [self setupLayout];
}

- (void)setupLayout {
    // 容器视图布局
    CGFloat containerWidth = self.bounds.size.width - 40;
    CGFloat containerHeight = 500;
    self.containerView.frame = CGRectMake(20, (self.bounds.size.height - containerHeight) / 2, containerWidth, containerHeight);
    
    // 标题布局
    self.titleLabel.frame = CGRectMake(0, 20, containerWidth, 30);
    
    // 描述文本布局
    self.descriptionLabel.frame = CGRectMake(20, CGRectGetMaxY(self.titleLabel.frame) + 10, containerWidth - 40, 20);
    
    // 息屏显示视图布局
    CGFloat screenDisplayViewHeight = 80;
    self.screenDisplayView.frame = CGRectMake(20, CGRectGetMaxY(self.descriptionLabel.frame) + 20,
                                              containerWidth - 40, screenDisplayViewHeight);
    
    // 息屏显示标签布局
    self.screenDisplayLabel.frame = CGRectMake(20, 15, 100, 25);
    
    // 息屏显示提示布局
    self.screenDisplayTipLabel.frame = CGRectMake(20, CGRectGetMaxY(self.screenDisplayLabel.frame), 150, 20);
    
    // 息屏显示开关布局
    self.screenDisplaySwitch.frame = CGRectMake(CGRectGetWidth(self.screenDisplayView.frame) - 70,
                                                (screenDisplayViewHeight - 30) / 2, 51, 31);
    
    // 显示为标签布局
    self.displayAsLabel.frame = CGRectMake(20, CGRectGetMaxY(self.screenDisplayView.frame) + 20, containerWidth - 40, 30);
    
    // 模式视图布局
    CGFloat modeViewWidth = (containerWidth - 40 - 20) / 3; // 三个视图，两个间隔
    CGFloat modeViewHeight = 90;
    CGFloat modeViewY = CGRectGetMaxY(self.displayAsLabel.frame) + 10;
    
    for (NSInteger i = 0; i < self.modeViews.count; i++) {
        UIView *modeView = self.modeViews[i];
        modeView.frame = CGRectMake(20 + i * (modeViewWidth + 10), modeViewY, modeViewWidth, modeViewHeight);
        
        // 示例视图布局
        UIView *exampleView = modeView.subviews.firstObject;
        exampleView.frame = CGRectMake(10, 10, modeViewWidth - 20, 40);
        
        // 模式标签布局
        UILabel *label = self.modeLabels[i];
        label.frame = CGRectMake(modeView.frame.origin.x, CGRectGetMaxY(modeView.frame) + 5, modeViewWidth, 20);
    }
    
    // 提示文本布局
    UIView * view = self.modeLabels.firstObject;
    self.tipLabel.frame = CGRectMake(20, CGRectGetMaxY(view.frame) + 20, containerWidth - 40, 20);
    
    // 确认按钮布局
    self.confirmButton.frame = CGRectMake(20, CGRectGetMaxY(self.tipLabel.frame) + 30, containerWidth - 40, 44);
    
    // 取消按钮布局
    self.cancelButton.frame = CGRectMake(20, CGRectGetMaxY(self.confirmButton.frame) + 10, containerWidth - 40, 44);
}

- (void)updateSelectedMode {
    for (NSInteger i = 0; i < self.modeViews.count; i++) {
        UIView *modeView = self.modeViews[i];
        UILabel *label = self.modeLabels[i];
        
        if (i == self.selectedDisplayMode) {
            modeView.backgroundColor = [UIColor colorWithRed:0/255.0 green:217/255.0 blue:147/255.0 alpha:0.1];
            modeView.layer.borderColor = [UIColor colorWithRed:0/255.0 green:217/255.0 blue:147/255.0 alpha:1.0].CGColor;
            label.textColor = [UIColor colorWithRed:0/255.0 green:217/255.0 blue:147/255.0 alpha:1.0];
        } else {
            modeView.backgroundColor = [UIColor whiteColor];
            modeView.layer.borderColor = [UIColor lightGrayColor].CGColor;
            label.textColor = [UIColor blackColor];
        }
    }
}

#pragma mark - Actions

- (void)screenSwitchChanged:(UISwitch *)sender {
    self.isScreenDisplayEnabled = sender.isOn;
}

- (void)modeViewTapped:(UITapGestureRecognizer *)sender {
    self.selectedDisplayMode = sender.view.tag;
    [self updateSelectedMode];
}

- (void)confirmButtonTapped {
    if (self.confirmBlock) {
        self.confirmBlock(self.isScreenDisplayEnabled, self.selectedDisplayMode);
    }
    [self dismiss];
}

#pragma mark - Show/Dismiss

- (void)show {
    // 获取当前活跃窗口的现代方法
    UIWindow *keyWindow = nil;
    
    if (@available(iOS 13.0, *)) {
        NSSet<UIScene *> *connectedScenes = [UIApplication sharedApplication].connectedScenes;
        for (UIScene *scene in connectedScenes) {
            if (scene.activationState == UISceneActivationStateForegroundActive && [scene isKindOfClass:[UIWindowScene class]]) {
                UIWindowScene *windowScene = (UIWindowScene *)scene;
                for (UIWindow *window in windowScene.windows) {
                    if (window.isKeyWindow) {
                        keyWindow = window;
                        break;
                    }
                }
                
                if (!keyWindow && windowScene.windows.count > 0) {
                    keyWindow = windowScene.windows.firstObject;
                }
                
                break;
            }
        }
    } else {
        keyWindow = [UIApplication sharedApplication].windows.firstObject;
    }
    
    // 如果还是没找到窗口，回退到旧方法
    if (!keyWindow) {
        keyWindow = [UIApplication sharedApplication].windows.firstObject;
    }
    
    [keyWindow addSubview:self];
    
    self.alpha = 0;
    self.containerView.transform = CGAffineTransformMakeScale(0.8, 0.8);
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
        self.containerView.transform = CGAffineTransformIdentity;
    }];
}

- (void)dismiss {
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
        self.containerView.transform = CGAffineTransformMakeScale(0.8, 0.8);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
